<template>
  <div class="index">
    <!-- 头部 -->
    <el-container>
      <el-header>
        <h1>电商管理系统</h1>
        <el-button type="info" @click="del_login">退出</el-button>
      </el-header>
    </el-container>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="width">
        <p @click="switchs" class="switchs">|||</p>
        <el-menu
          default-active="1"
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          :collapse="isColl"
          router
        >
          <el-submenu
            :index="index + ''"
            v-for="(item, index) in Menus"
            :key="index"
          >
            <template slot="title">
              <i :class="icon[item.id]"></i>
              <span>{{ item.authName }}</span></template
            >
            <el-menu-item
              :class="{ active: $route.path == '/' + subItem.path }"
              @click="addRoute(item.authName, subItem.authName)"
              :index="subItem.path"
              v-for="(subItem, subIndex) in item.children"
              :key="subIndex"
              ><i class="el-icon-menu"></i
              ><span>{{ subItem.authName }}</span></el-menu-item
            >
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      width: "200px", //默认侧边栏宽度
      isColl: false,
      Menus: [],
      icon: {
        125: "el-icon-user-solid",
        103: "el-icon-s-shop",
        101: "el-icon-s-cooperation",
        102: "el-icon-s-claim",
        145: "el-icon-s-comment",
      },
    };
  },
  methods: {
    //退出登录
    del_login() {
      localStorage.removeItem("token");
      this.$router.push("/login");
    },
    //  请求侧边栏数据
    getMenus() {
      this.$axios.get("/menus").then((res) => {
        console.log(res);
        this.Menus = res.data.data;
        console.log(this.Menus);
      });
    },
    //展开收起侧边栏
    switchs() {
      if (this.isColl) {
        this.isColl = false;
        this.width = "200px";
      } else {
        this.isColl = true;
        this.width = "60px";
      }
    },
  },
  created() {
    this.getMenus();
  },
  mounted() {},
  components: {},
  computed: {},
  watch: {},
};
</script>

<style lang='scss' scoped>
.index {
  width: 100%;
  height: 100vh;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #3a3d42;
  color: #fff;
}
.el-aside {
  height: 91vh;
  background-color: #545c64;
}
.switchs {
  background: #4b5062;
  color: #fff;
  text-align: center;
  width: 100%;
}
</style>
